<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>1024博客-找回密码</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}"  media="all">
    <link rel="stylesheet" th:href="@{/assets/css/toastr.css}" >
</head>
<body th:background="@{/assets/img/back.jpg}">
<!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
<!--[if lt IE 9]>
<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

<div class="layui-container">
    <div class="layui-row layui-col-space10">
        <div class="layui-col-xs6 layui-col-sm6 layui-col-md4 ">
            <div></div>
        </div>
        <div class="layui-col-xs6 layui-col-sm6 layui-col-md4" >
            <form class="layui-form layui-font-gray layui-anim layui-anim-scaleSpring" lay-filter="example" id="registerForm" style="border: 1px solid  blue;vertical-align: middle;margin-top: 200px">
                <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
                    <legend>1024博客 -找回密码</legend>
                </fieldset>
                <div class="layui-form-item">
                    <label class="layui-form-label ">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" lay-verify="required" lay-reqText="请输入用户名"	autocomplete="off" placeholder="请输入用户名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-block">
                        <input type="email" name="email" placeholder="请输入邮箱" lay-verify="required" lay-reqText="请输入邮箱" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">验证码</label>
                    <div class="layui-input-block">
                        <input type="text" name="code" placeholder="请输入验证码"  lay-reqText="请输入验证码" autocomplete="off" class="layui-input">
                        <button type="submit" class="layui-btn layui-anim layui-anim-scaleSpring" lay-submit="" lay-filter="sendCode">发送验证码</button>
                    </div>
                    <div class="layui-input-block">

                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">新密码</label>
                    <div class="layui-input-block">
                        <input type="password" name="password" placeholder="请确认输入密码"  autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="submit" class="layui-btn layui-anim layui-anim-scaleSpring" lay-submit="" lay-filter="findPassword">提交</button>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <a href="/login">
                            <button type="button" class="layui-btn layui-btn-xs  layui-btn-radius  layui-btn-normal" >登录</button>
                        </a>
                        <a href="/register">
                            <button type="button" class="layui-btn layui-btn-xs  layui-btn-radius  layui-btn-normal" >注册</button>
                        </a>
                    </div>

                </div>
            </form>
        </div>
        <div class="layui-col-xs4 layui-col-sm12 layui-col-md4">
            <div></div>
        </div>
    </div>
</div>
<script th:src="@{/assets/js/jquery.min.js}"></script>
<script th:src="@{/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/assets/js/toastr.min.js}"></script>
<script>
    layui.use(['form'], function(){
        var form = layui.form
            ,layer = layui.layer;

        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 5){
                    return '标题至少得5个字符啊';
                }
            }
            ,passwordRule: [
                /^[\S]{8,13}$/
                ,'密码必须8到13位，且不能出现空格'
            ]
        });

        form.on('submit(findPassword)', function(data){
            findPassword()
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        form.on('submit(sendCode)', function(data){
            sendCode()
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

    });

    function findPassword(){
        var params = $("#registerForm").serialize();
        var url = '/forget';
        $.ajax({
            type: 'POST',//方法类型
            url: url,
            data: params,
            dataType:'json',
            success: function (result) {
                if(result.success){
                    toastr.success("找回成功，请返回登录！")
                }else{
                    toastr.warning(result.msg)
                }
            },
            error: function () {
                toastr.error('提交失败，请检查网络连接')
            }
        });
    }
    function sendCode(){
        var params = $("#registerForm").serialize();
        var url = '/forget/code';
        $.ajax({
            type: 'POST',//方法类型
            url: url,
            data: params,
            dataType:'json',
            success: function (result) {
                if(result.success){
                    toastr.success("验证码发送成功，有效期为5分钟！请进入邮箱查看")
                }else{
                    toastr.warning(result.msg)
                }
            },
            error: function () {
                toastr.error('提交失败，请检查网络连接')
            }
        });
    }
</script>

</body>
</html>